<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="deque-objects.html" />
<link rel="parent" href="module-collections.html" />
<link rel="next" href="defaultdict-examples.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>5.3.2 defaultdict objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.3.1.1 Recipes"
  href="deque-recipes.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.3 collections  "
  href="module-collections.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.3.2.1 defaultdict Examples"
  href="defaultdict-examples.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="deque-recipes.html">5.3.1.1 Recipes</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-collections.html">5.3 collections  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="defaultdict-examples.html">5.3.2.1 defaultdict Examples</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION007320000000000000000"></a><a name="defaultdict-objects"></a>
<br>
5.3.2 <tt class="class">defaultdict</tt> objects 
</h2>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-743' xml:id='l2h-743' class="function">defaultdict</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>default_factory</var><big>[</big><var>, ...</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Returns a new dictionary-like object.  <tt class="class">defaultdict</tt> is a subclass
  of the builtin <tt class="class">dict</tt> class.  It overrides one method and adds one
  writable instance variable.  The remaining functionality is the same as
  for the <tt class="class">dict</tt> class and is not documented here.

<p>
The first argument provides the initial value for the
  <tt class="member">default_factory</tt> attribute; it defaults to <code>None</code>.
  All remaining arguments are treated the same as if they were
  passed to the <tt class="class">dict</tt> constructor, including keyword arguments.

<p>

<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
<tt class="class">defaultdict</tt> objects support the following method in addition to
the standard <tt class="class">dict</tt> operations:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-744' xml:id='l2h-744' class="method">__missing__</tt></b>(</nobr></td>
  <td><var>key</var>)</td></tr></table></dt>
<dd>
  If the <tt class="member">default_factory</tt> attribute is <code>None</code>, this raises
  an <tt class="exception">KeyError</tt> exception with the <var>key</var> as argument.

<p>
If <tt class="member">default_factory</tt> is not <code>None</code>, it is called without
  arguments to provide a default value for the given <var>key</var>, this
  value is inserted in the dictionary for the <var>key</var>, and returned.

<p>
If calling <tt class="member">default_factory</tt> raises an exception this exception
  is propagated unchanged.

<p>
This method is called by the <tt class="method">__getitem__</tt> method of the
  <tt class="class">dict</tt> class when the requested key is not found; whatever it
  returns or raises is then returned or raised by <tt class="method">__getitem__</tt>.
</dl>

<p>
<tt class="class">defaultdict</tt> objects support the following instance variable:

<p>
<dl><dt><b><tt id='l2h-745' xml:id='l2h-745'>default_factory</tt></b></dt>
<dd>
  This attribute is used by the <tt class="method">__missing__</tt> method; it is initialized
  from the first argument to the constructor, if present, or to <code>None</code>, 
  if absent.
</dd></dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="defaultdict-examples.html">5.3.2.1 <tt class="class">defaultdict</tt> Examples</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.3.1.1 Recipes"
  href="deque-recipes.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5.3 collections  "
  href="module-collections.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.3.2.1 defaultdict Examples"
  href="defaultdict-examples.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="deque-recipes.html">5.3.1.1 Recipes</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-collections.html">5.3 collections  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="defaultdict-examples.html">5.3.2.1 defaultdict Examples</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
